<?php

use Biz\cache\wiki\Wiki as cWiki;
use Biz\service\wiki\WikiBuyService;
use Doraemon\tools\ActionLog as tActionLog;
class UnBuyAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;

    public function main()
    {
        $uid = $this->uid;
        if (!$uid) {
            throw new \Exception("请登录重试！");
        }

        $wikiId = $this->getRequest()->getPost('wiki_id', 0);
        if ($wikiId == 0) {
            throw new \Exception("参数错误！");
        }
        //5秒禁止提交
        $lockKey = 'wiki:un_buy:uid_'.$uid.'-'.$wikiId;
        $lock = cWiki::isLock($lockKey,5);
        if ($lock) {
            throw new \Exception('操作过于频繁，请稍后再试');
        }
        WikiBuyService::unWikiBuy($uid, $wikiId);
        tActionLog::log($this->uid, [
            'uid' => $this->uid,
            'wiki_id' => $wikiId,
        ], tActionLog::BIZ_WIKI_UNBOUGHT);

        $this->setSuccess(null, self::API_CODE_SUCCES);
    }
}